<template>
  <div class="goodsDetails">
    <!-- 头部 -->
    <div class="page-header">
      <div class="title">
        <p>Redmi K30</p>
        <div class="list">
          <ul>
            <li>
              <router-link to>概述</router-link>
            </li>
            <li>
              <router-link to>参数</router-link>
            </li>
            <li>
              <router-link to>用户评价</router-link>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <!-- 头部END -->

    <!-- 主要内容 -->
    <div class="main">
      <div class="block">
        <el-carousel
          indicator-position=""
          height="560px"
          v-if="goodsImg.length > 1"
        >
          <el-carousel-item v-for="item in goodsImg" :key="item.intro">
            <img v-lazy="item.product_picture" alt="" />
          </el-carousel-item>
        </el-carousel>
        <div v-if="goodsImg.length == 1">
          <img
            style="height: 560px"
            v-lazy="goodsImg[0].product_picture"
            :alt="goodsImg[0].intro"
          />
        </div>
      </div>
      <!-- 左侧商品轮播图END -->

      <!-- 右侧内容区 -->
      <div class="content">
        <h1 class="name">{{ this.goodsData.product_name }}</h1>
        <p class="intro">{{ this.goodsData.product_intro }}</p>
        <p class="store">小米自营</p>
        <div class="price">
          <span>{{ this.goodsData.product_selling_price }}元</span>
          <span
            v-show="
              this.goodsData.product_price !=
              this.goodsData.product_selling_price
            "
            class="del"
            >{{ this.goodsData.product_price }}元</span
          >
        </div>
        <div class="pro-list">
          <span class="pro-name">{{ this.goodsData.product_name }}</span>
          <span class="pro-price">
            <span>{{ this.goodsData.product_selling_price }}元</span>
            <span
              v-show="
                this.goodsData.product_price !=
                this.goodsData.product_selling_price
              "
              class="pro-del"
              >{{ this.goodsData.product_price }}元</span
            >
          </span>
          <p class="price-sum">
            总计 :{{ this.goodsData.product_selling_price }} 元
          </p>
        </div>
        <!-- 内容区底部按钮 -->
        <div class="button">
          <el-button class="shop-cart" @click="addGoods">加入购物车</el-button>
          <el-button class="like">喜欢</el-button>
        </div>
        <!-- 内容区底部按钮END -->
        <div class="pro-policy">
          <ul>
            <li><i class="el-icon-circle-check"></i> 小米自营</li>
            <li><i class="el-icon-circle-check"></i> 小米发货</li>
            <li><i class="el-icon-circle-check"></i> 7天无理由退货</li>
            <li><i class="el-icon-circle-check"></i> 7天价格保护</li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapActions, mapGetters } from "vuex";
export default {
  data() {
    return {
      product_id: this.$route.query.product_id,
      goodsImg: [],
      goodsData: [],
    };
  },
  created() {
    this.getGoodsImg();
    this.getGoodsData();
  },
  methods: {
    ...mapActions("cart", ["unshiftShoppingCart", "addShoppingCartNum"]),
    async getGoodsImg() {
      const res = await this.$http.getGoodsImg();
      let arr = [];
      res.data.forEach((item) => {
        if (item.product_id === Number(this.product_id)) {
          arr.push(item);
        }
      });
      this.goodsImg = arr;
    },
    async getGoodsData() {
      const res = await this.$http.getGoodsData();
      this.goodsData = res.data.find((item) => {
        return item.product_id === Number(this.product_id);
      });
    },
    async addGoods() {
      if (!JSON.parse(sessionStorage.getItem("user"))) {
        this.$notify({
          title: "失败",
          message: "请先登录再添加至购物车",
          type: "error",
          duration: "1500",
        });
        return;
      }
      let res = await this.$http.getShoppingCart();
      let arr = res.data;
      let goodData = {
        id: Date.now(),
        product_id: Number(this.product_id),
        user_id: JSON.parse(sessionStorage.getItem("user")).id,
        product_name: this.goodsData.product_name,
        product_selling_price: this.goodsData.product_selling_price,
        product_picture: this.goodsImg[0].product_picture,
        count: 1,
        checked: false,
      };
      if (arr.length != 0) {
        let addFlag = arr.some((item) => {
          return (
            item.product_id == goodData.product_id &&
            item.user_id == goodData.user_id
          );
        });
        if (addFlag) {
          let obj = arr.find((item) => {
            return (
              item.product_id == goodData.product_id &&
              item.user_id == goodData.user_id
            );
          });
          let goodsNum = obj.count + 1;
          let res = await this.$http.patchShoppingCart({
            id: obj.id,
            count: goodsNum,
          });
        } else {
          let res = await this.$http.postShoppingCart(goodData);
          this.addShoppingCartNum(res.data);
        }
      } else {
        let res = await this.$http.postShoppingCart(goodData);
        this.unshiftShoppingCart(res.data);
      }
      this.$notify({
        title: "成功",
        message: "添加购物车成功",
        type: "success",
        duration: "1500",
      });
    },
  },
  computed: {
    ...mapGetters({
      count: "cart/getNum",
    }),
  },
};
</script>
<style lang="scss">
.goodsDetails {
  p {
    margin: 0 !important;
  }
  .page-header {
    height: 64px;
    z-index: 4;
    margin-top: -20px;
    background: #fff;
    border-bottom: 1px solid #e0e0e0;
    box-shadow: 0px 5px 5px rgba(0, 0, 0, 0.07);
    .title {
      width: 1225px;
      height: 64px;
      line-height: 64px;
      font-size: 18px;
      font-weight: 400;
      color: #212121;
      text-align: center;
      margin: 0 auto;
      p {
        float: left;
      }
      .list {
        height: 64px;
        float: right;
        li {
          float: left;
          margin-left: 20px;
          a {
            font-size: 14px;
            color: #616161;
          }
          a:hover {
            font-size: 14px;
            color: #ff6700;
          }
        }
      }
    }
  }
  .main {
    width: 1225px;
    height: 560px;
    padding-top: 30px;
    margin: 0 auto;
    .block {
      float: left;
      width: 560px;
      height: 560px;
      .el-carousel__button {
        background-color: rgba(163, 163, 163, 0.8);
      }
      img {
        height: 560px;
      }
    }
    .content {
      float: left;
      margin-left: 25px;
      width: 640px;
      .name {
        height: 30px;
        line-height: 30px;
        font-size: 24px;
        font-weight: normal;
        color: #212121;
      }
      .intro {
        color: #b0b0b0;
        padding-top: 10px;
      }
      .store {
        color: #ff6700;
        padding-top: 10px;
      }
      .price {
        display: block;
        font-size: 18px;
        color: #ff6700;
        border-bottom: 1px solid #e0e0e0;
        padding: 25px 0 25px;
        .del {
          font-size: 14px;
          margin-left: 10px;
          color: #b0b0b0;
          text-decoration: line-through;
        }
      }
      .pro-list {
        background: #f9f9fa;
        padding: 30px 60px;
        margin: 30px 0 30px;
        span {
          line-height: 30px;
          color: #616161;
        }
        .pro-price {
          float: right;
          .pro-del {
            margin-left: 10px;
            text-decoration: line-through;
          }
        }
        .price-sum {
          color: #ff6700;
          font-size: 24px;
          padding-top: 20px;
        }
      }
      .button {
        height: 55px;
        margin: 0px 0 20px 0;
        .el-button {
          float: left;
          height: 55px;
          font-size: 16px;
          color: #fff;
          border: none;
          text-align: center;
        }
        .shop-cart {
          width: 340px;
          background-color: #ff6700;
        }
        .shop-cart:hover {
          background-color: #f25807;
        }
        .like {
          width: 260px;
          margin-left: 40px;
          background-color: #b0b0b0;
        }
        .like:hover {
          background-color: #757575;
        }
      }
      .pro-policy li {
        float: left;
        margin-right: 20px;
        color: #b0b0b0;
      }
    }
  }
}
</style>
